﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PreeRegisterModel;
using System.Data;
using PreeRegisterDAL;

namespace PreeRegisterBLL
{
    public class WorkDetailBLL
    {
        /// <summary>
        /// 排班号源排查
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static WorkDetailResponseModel GetWorkDetail(WorkDetailRequestModel request)
        {
            try
            {
                //查询排班号源
                WorkDetailResponseModel workDetailResponseModel = new PreeRegisterModel.WorkDetailResponseModel();

                if (request.workdate.Trim() == "")
                {
                    return null;
                }

                //增加同煤版  liujia150812
                DataSet ds = WorkDetailDAL.GetWorkDetailInfo(VariableBLL.IFTmkyy,request.workdate.Trim());

                //赋值排班号源实体
                List<WorkDetaildept> WorkDetaildept_list = new List<WorkDetaildept>();

                //科室
                foreach (DataRow drDictDept in ds.Tables["DictDept"].Rows)
                {
                    WorkDetaildept workDetaildept = new WorkDetaildept();

                    string ideptno = drDictDept["iDeptID"].ToString();
                    DataView dvWorkPlan = new DataView(ds.Tables["OPDWorkPlan"]);
                    dvWorkPlan.RowFilter = "iDeptID=" + ideptno;
                    List<WorkDetailWork> WorkDetailWork_list = new List<WorkDetailWork>();
                    //排班
                    foreach (DataRowView drvWorkPlan in dvWorkPlan)
                    {
                        WorkDetailWork workDetailWork = new WorkDetailWork();

                        string iworkplanid = drvWorkPlan["iWorkPlanID"].ToString();
                        DataView dvWorkPlanDetail = new DataView(ds.Tables["OPDWorkPlanDetail"]);
                        dvWorkPlanDetail.RowFilter = "iWorkPlanID=" + iworkplanid;

                        List<WorkDetailWorkno> WorkDetailWorkno_list = new List<WorkDetailWorkno>();

                        //号源
                        foreach (DataRowView drvWorkPlanDetail in dvWorkPlanDetail)
                        {
                            WorkDetailWorkno workDetailWorkno = new WorkDetailWorkno();

                            workDetailWorkno.resourceid = drvWorkPlanDetail["iDetailId"].ToString();
                            workDetailWorkno.state = drvWorkPlanDetail["istatus"].ToString();

                            WorkDetailWorkno_list.Add(workDetailWorkno);
                        }

                        workDetailWork.wid = iworkplanid;
                        workDetailWork.nos = WorkDetailWorkno_list;

                        WorkDetailWork_list.Add(workDetailWork);
                    }

                    workDetaildept.deptid = ideptno;
                    workDetaildept.works = WorkDetailWork_list;
                    WorkDetaildept_list.Add(workDetaildept);
                }

                workDetailResponseModel.hospid = VariableBLL.HospitalId;
                workDetailResponseModel.atype = Convert.ToString((VariableBLL.bPreTime) ? 1 : 0);
                workDetailResponseModel.depts = WorkDetaildept_list;

                //返回
                return workDetailResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }
    }
}
